package unionpay

func parseDataToMap(data []byte) map[string]string {
	var (
		result     = map[string]string{}
		iskey      = true
		isOpen     = false
		closeKey   byte
		temp       []byte
		key, value string
	)
	for data[0] == '{' || data[0] == '[' {
		data = data[1:len(data)]
	}
	for _, v := range data {

		if iskey { // key 处理
			if v == '=' {
				key = string(temp)
				temp = []byte{}
				iskey = false
			} else {
				temp = append(temp, v)
			}
		} else { // value 处理
			if isOpen {
				if v == closeKey {
					isOpen = false
				}
			} else {
				if v == '{' || v == '[' {
					isOpen = true
					closeKey = v
				}
			}

			if v == '&' && !isOpen {
				value = string(temp)
				temp = []byte{}
				if key != "" {
					result[key] = value
				}
			} else {
				temp = append(temp, v)
			}
		}
	}
	return result
}
